clear all

cd "~/Dropbox (Harvard University)/Monetary Policy with Heterogenous Producers/Replication Code"

ssc install grstyle, replace
grstyle init
grstyle set plain, nogrid
graph set window fontface "Palatino"

* Annual GW shocks
import excel "data/Gorodnichenko_Weber_2016/Gorodnichenko_Weber.xlsx", sheet("Sheet2") firstrow clear
drop if DATE==.
g year_start = dofy(yofd(DATE))
g year_end = dofy(yofd(DATE) + 1)
format year_start %td 
format year_end %td
g phi = (year_end - DATE) / (year_end - year_start)
g tight_surprise_phiwt = phi*tight_suprise 
g wide_surprise_phiwt = phi*wide_surprise 
g tight_surprise_antiphiwt = (1-phi)*tight_suprise
g wide_surprise_antiphiwt = (1-phi)*wide_surprise
collapse (sum) tight_surprise_phiwt wide_surprise_phiwt tight_surprise_antiphiwt wide_surprise_antiphiwt, by(year_start)
g year = yofd(year_start)
tsset year
g tight_surprise = tight_surprise_phiwt + L1.tight_surprise_antiphiwt
g wide_surprise = wide_surprise_phiwt + L1.wide_surprise_antiphiwt
replace tight_surprise = tight_surprise_phiwt if tight_surprise==.
replace wide_surprise = wide_surprise_phiwt if wide_surprise==.
keep year tight_surprise wide_surprise
save "data/Gorodnichenko_Weber_2016/GW_clean_annual", replace

* Census concentration ratios
import delimited using "data/Census_Mfg_Concentration/2007_31-33_Con_Ratios_US/ECN_2007_US_31SR12_with_ann.csv", clear varnames(1) rowrange(3:)
g naics_level = strlen(naicsid)
drop if concenfiid=="004"
g n_firms_ratio = 4
replace n_firms_ratio = 8 if concenfiid=="857"
replace n_firms_ratio = 20 if concenfiid=="858"
replace n_firms_ratio = 50 if concenfiid=="859"
destring ccorcppct, gen(pct) force
keep naicsid naicsdisplaylabel naics_level n_firms_ratio pct
reshape wide pct, i(naicsid naicsdisplaylabel naics_level) j(n_firms_ratio)
keep if naics_level==4
drop naics_level
rename naicsid naics_code 
save "data/Census_Mfg_Concentration/concentration_ratios_2007", replace

import delimited using "data/Census_Mfg_Concentration/2002_31-33_Con_Ratios_US/ECN_2002_US_31SR12_with_ann.csv", clear varnames(1) rowrange(3:)
g naics_level = strlen(naicsid)
drop if concenfiid=="004"
g n_firms_ratio = 4
replace n_firms_ratio = 8 if concenfiid=="857"
replace n_firms_ratio = 20 if concenfiid=="858"
replace n_firms_ratio = 50 if concenfiid=="859"
destring ccorcppct, gen(pct) force
keep naicsid naicsdisplaylabel naics_level n_firms_ratio pct
reshape wide pct, i(naicsid naicsdisplaylabel naics_level) j(n_firms_ratio)
keep if naics_level==4
drop naics_level
rename naicsid naics_code 
save "data/Census_Mfg_Concentration/concentration_ratios_2002", replace


* BLS multifactor productivity data
use "data/BLS_Productivity/BLS_annual_prod_clean", clear
drop index

merge m:1 year using "data/Monetary_shocks/RR_monetary_shock_annual"
tab year if _merge==2
drop if _merge==2
drop _merge
merge m:1 year using "data/Gorodnichenko_Weber_2016/GW_clean_annual"
tab year if _merge==2
drop if _merge==2
drop _merge

* Limit to NAICS4 industries
keep if display_level==3
encode industry_code, gen(industry_code_numeric)
sort industry_code_numeric year
xtset industry_code_numeric year

local hmax = 4
local cycle_var resid_full
forvalues h = 0/`hmax' {
	gen cum`h'_log_prod = F`h'.log_prod - L1.log_prod
	gen cum`h'_log_output = F`h'.log_output - L1.log_output
	gen cum`h'_prod_output_ratio = cum`h'_log_prod / cum`h'_log_output
	winsor2 cum`h'_prod_output_ratio, replace cuts(5 95)
	gen est`h'_ir = .
	gen se`h'_ir = .
}

local concentration_year = 2002
merge m:1 naics_code using "data/Census_Mfg_Concentration/concentration_ratios_`concentration_year'"
drop _merge
// merge m:1 naics_code using "data/Pasten_Schoenle_Weber/PSW_FPA_clean.dta"
// drop _merge

sort industry_code_numeric year
xtset industry_code_numeric year
local n_firm_list 8 20 50
est clear
foreach n_firm in `n_firm_list' {
	g pct`n_firm'_full = pct`n_firm'/100
	g cycle_pct`n_firm'_inter = `cycle_var' * pct`n_firm'_full
	regress cum0_log_prod pct`n_firm'_full cycle_pct`n_firm'_inter L(0/2).`cycle_var' L(1/2).log_prod, vce(robust)
	reghdfe cum0_log_prod pct`n_firm'_full cycle_pct`n_firm'_inter L(0/2).`cycle_var' L(1/2).log_prod, absorb(naics_code year) vce(robust)
	eststo r`n_firm'
}
esttab r*, keep(cycle_pct*_inter) star("*" 0.10 "**" 0.05) se nomtitles nonotes
esttab r* using "outputs/industry_prod_`cycle_var'_`concentration_year'.tex", replace booktabs keep(cycle_pct*_inter) star("*" 0.10 "**" 0.05) se nomtitles nonotes

